package cn.javano.smartweb.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * @author: wangjie19558
 * Date:2016/11/1
 * Description:切面代理模板
 */
public abstract class AspectProxy implements Proxy {
    private static final Logger logger = LoggerFactory.getLogger(AspectProxy.class);

    public Object doProxy(ProxyChain proxyChain) throws Throwable {
        Object result = null;

        Class<?> clazz = proxyChain.getTargetClass();
        Method method = proxyChain.getTargetMethod();
        Object[] objects = proxyChain.getMethodParams();

        begin();
        if (intercept(clazz, method, objects)) {
            before(clazz, method, objects);
            result = proxyChain.doProxyChain();
            after(clazz, method, objects, result);
        } else {
            result = proxyChain.doProxyChain();
        }
        end();
        return null;
    }


    public void begin() {
    }


    public boolean intercept(Class<?> clazz, Method method, Object[] objects) throws Throwable {
        return true;
    }

    public void before(Class<?> clazz, Method method, Object[] objects) throws Throwable {

    }

    public void after(Class<?> clazz, Method method, Object[] objects, Object result) throws Throwable {

    }

    public void error(Class<?> clazz, Method method, Object[] objects, Throwable e) {

    }

    public void end() {
    }

}
